package com.lzb.compressmaster.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.FileUtils;
import com.bumptech.glide.Glide;
import com.lzb.compressmaster.R;

import java.util.Collections;
import java.util.List;

public class ImgSortAdapter extends RecyclerView.Adapter<ImgSortAdapter.ViewHolder> {
    private List<String> mDatas;

    public ImgSortAdapter(List<String> items) {
        mDatas = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_img_sort, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Glide.with(holder.itemView).load(mDatas.get(position)).into(holder.ivImg);
        holder.tvName.setText(FileUtils.getFileName(mDatas.get(position)));
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    // 拖动排序逻辑
    public void onItemMove(int fromPos, int toPos) {
        Collections.swap(mDatas, fromPos, toPos);
        notifyItemMoved(fromPos, toPos);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivImg;
        TextView tvName;

        ViewHolder(View itemView) {
            super(itemView);
            ivImg = itemView.findViewById(R.id.ivImgSortItemImg);
            tvName = itemView.findViewById(R.id.tvImgSortItemName);
        }
    }
}
